package com.koron.order.grade.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.order.grade.bean.entity.GradeFormBean;
import com.koron.order.grade.bean.entity.OrderGradeBean;
import com.koron.order.grade.bean.query.OrderGradeQuery;
import com.koron.order.grade.bean.vo.GradeFormVO;
import com.koron.order.grade.bean.vo.OrderGradeVO;
import com.koron.order.grade.service.GradeFormService;
import com.koron.order.grade.service.OrderGradeService;
import com.koron.order.overhaul.utils.CommonUtils;
import com.koron.order.overhaul.utils.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: OrderGradeController
 * @projectName GDH_EAM_V2
 * @description: 分级分项验收单
 * @date 2021/10/1114:18
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "分级分项验收单")
public class OrderGradeController {

    @Autowired
    private OrderGradeService orderGradeService;

    @Autowired
    private GradeFormService gradeFormService;

    @ApiOperation(value = "根据id查询详情")
    @GetMapping("/grade/queryById")
    @ApiParam(name = "id", value = "分级分项验收单id")
    public Response<OrderGradeVO> queryById(@CasUser @ApiIgnore EamUser user, String id) {
        log.info("分级分项验收单详情查询， id = {}", id);
        return Response.ok("查询成功", ADOConnection.runTask(user.getCurrDs(), orderGradeService, "queryById", OrderGradeVO.class, id));
    }

    @ApiOperation(value = "新增或修改, 返回id")
    @PostMapping("/grade/saveOrUpdate")
    public Response<String> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @RequestBody OrderGradeVO vo) {
        log.info("分级分项验收单编辑，vo = {}", JSON.toJSONString(vo));
        vo.setDeptId(user.getDeptId());
        CommonUtils.update(vo, user);
        vo = ADOConnection.runTask(user.getCurrDs(), orderGradeService, "saveOrUpdate", OrderGradeVO.class, vo);
        return Response.ok("操作成功", vo.getId());
    }

    @ApiOperation(value = "根据id删除分级分项验收单")
    @ApiParam(value = "分级分项验收单id")
    @DeleteMapping("/grade/deleteById")
    public Response<OrderGradeBean> deleteById(@CasUser @ApiIgnore EamUser user, String id) {
        log.info("分级分项验收单删除，id = {}", id);
        return Response.ok("删除成功", ADOConnection.runTask(user.getCurrDs(), orderGradeService, "deleteById", OrderGradeBean.class, id));
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/grade/queryList")
    public Response<PageInfo<OrderGradeVO>> queryList(@CasUser @ApiIgnore EamUser user, OrderGradeQuery query) {
        log.info("分级分项验收单列表查询，query = {}", JSON.toJSONString(query));
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List list = ADOConnection.runTask(user.getCurrDs(), orderGradeService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @ApiOperation(value = "更新分级分项验收单排序")
    @PostMapping("/grade/updateSortById")
    public Response<Integer> updateSortById(@CasUser @ApiIgnore EamUser user, @RequestBody List<OrderGradeBean> beans) {

        return Response.ok("查询成功", ADOConnection.runTask(user.getCurrDs(), orderGradeService, "updateSortById", Integer.class, beans));
    }

    //@ApiOperation(value = "新增验收单和表单关系")
    //@PostMapping("/gradeForm/save")
    @Deprecated
    public Response<List<GradeFormVO>> saveGradeForm(@CasUser @ApiIgnore EamUser user, @RequestBody List<GradeFormBean> beans) {
        log.info("新增试验单和表单关系，beans = {}", JSON.toJSONString(beans));
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), gradeFormService, "saveOrUpdate", List.class, beans));
    }

    //@ApiOperation(value = "获取验收单和表单关系")
    //@GetMapping("/gradeForm/list")
    @Deprecated
    public Response<List<GradeFormVO>> list(@CasUser @ApiIgnore EamUser user, GradeFormVO query) {
        log.info("获取试验单和表单关系，query = {}", JSON.toJSONString(query));
        return Response.ok("查询成功", ADOConnection.runTask(user.getCurrDs(), gradeFormService, "queryList", List.class, query));
    }

    @PostMapping("/grade/workflow/workflowUpdate")
    @ApiOperation("修改业务状态, 非前端接口")
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        OrderGradeBean bean = JSON.parseObject(redirectQuery.getFormVariables(), OrderGradeBean.class);
        log.info("业务单id: {}, 工作流节点提交", bean.getId());
        return Response.success();
    }

    @PostMapping("/grade/workflow/workflowUpdateEnd")
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response updateEnd(@RequestBody RedirectQuery redirectQuery) {
        OrderGradeVO bean = JSON.parseObject(redirectQuery.getFormVariables(), OrderGradeVO.class);
        log.info("业务单id: {}, 工作流结束", bean.getId());
        // 工作流结束后，状态调整为"已归档"
        bean.setStatus(Constants.OrderGradeStatus.FINISH);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), orderGradeService, "saveOrUpdate", OrderGradeVO.class, bean);
        return Response.success();
    }

    @PostMapping("/grade/workflow/workflowUpdateCancel")
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response updateCancel(@RequestBody RedirectQuery redirectQuery) {
        OrderGradeBean vo = JSON.parseObject(redirectQuery.getFormVariables(), OrderGradeBean.class);
        log.info("业务单id: {}, 工作流作废", vo.getId());
        return Response.success();
    }
}
